//
//  KKStatusOriginalView.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/27.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit
import SnapKit
import SVProgressHUD
//设置对齐的边距
let StatusCellMargin: CGFloat = 8
//定义原创微博正文的字号
let originalContentLabelFontSize: CGFloat = 14

class KKStatusOriginalView: UIView {

    var bottomCons: Constraint?
    var statusViewModel: KKStatusViewModel? {
        didSet {
            // 数据绑定
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLable.text = statusViewModel?.time
            
            sourceLabel.text = statusViewModel?.sourceText
            contentLabel.attributedText = statusViewModel?.originalAttrbuiteString

            
            // 认证图片
            verifiedImageView.image = statusViewModel?.veritiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage

            //给配图视图设置数据源
            //pictureView.pictures = statusViewModel?.status?.pic_urls
            //在设置数据源之前根据图片的数量 来判断是否需要调整 '自己的底部'约束
            //在更新约束之前需要卸载之前记录的约束
            bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                //有配图的原创微博
                //设置数据
                pictureView.pictures = urls
                //修改底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图的原创微博
                
                //设置数据
                pictureView.pictures = nil
                //修改约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }

        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.yellowColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //手写代码创建的视图的背景色 是头透明的
        sepView.backgroundColor = UIColor.redColor()
        addSubview(sepView)
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            //设置高度
            make.height.equalTo(StatusCellMargin)
        }
        headIcon.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.width.height.equalTo(40)
        }
        verifiedImageView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headIcon)
        }
        mbrankImageView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel)
        }
        timeLable.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLable.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLable)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon)
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            //设置测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) in
           self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        //实现yylabel高亮状态下的点击事件的回调
        //(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);
        contentLabel.highlightTapAction = { (containerView: UIView,text: NSAttributedString,range: NSRange,rect: CGRect) -> () in
            //通过回调的属性字符串 和range 来截取用户点击的文本
            let str = text.string
            let subStr = (str as NSString).substringWithRange(range)
            print(subStr)
            SVProgressHUD.showWithStatus(subStr)
        }

    }
    
    
    /// 懒加载子控件
    //懒加载
    //分隔条
    private lazy var sepView: UIView = UIView()
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证的头像
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var timeLable: UILabel = UILabel(title: "11:11", textColor: UIColor.orangeColor(), font: 10)
    private lazy var sourceLabel: UILabel = UILabel(title: "来自火星网友", textColor: UIColor.darkGrayColor(), font: 10)
    private lazy var nameLabel: UILabel = UILabel(title: "KingKong", textColor: UIColor.darkGrayColor(), font: 14)
    private lazy var contentLabel: YYLabel = {
        let yy = YYLabel()
        //设置最大的布局宽度
        yy.preferredMaxLayoutWidth = ScreenWidth - 2 * StatusCellMargin
        //设置自动换行
        yy.numberOfLines = 0
        //yy.textColor = UIColor.redColor()
        return yy
    }()


    //配图视图
    private lazy var pictureView: KKStatusPictureView = KKStatusPictureView()

}
